package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reggie.common.R;
import com.reggie.entity.AddressBook;
import com.reggie.entity.User;
import com.reggie.service.AddressBookService;
import com.sun.prism.impl.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 收货地址
 * @program: reggie
 * @author:
 * @create: 2022-10-12 15:53
 **/
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取默认地址
     * AddressBook的1
     * */
    @GetMapping("/default")
    public R<AddressBook> getDefault(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1);
        wrapper.eq(AddressBook::getUserId,userId);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return R.success(addressBook);
    }
    /**
     * 获取所有的收货地址
     * */
    @GetMapping("/list")
    public R<List> list(HttpSession session){
        //根据当前用户的id进行获取收货地址
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }
    /**
     * 添加收货地址
     * */
    @PostMapping
    public R<String> addressBook(@RequestBody AddressBook addressBook, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加成功！");
    }

    /**
     * 修改默认的收货地址
     * 需要将其他的地址不是默认的地址
     * */
    @PutMapping("/default")
    public R<String> putDefault(@RequestBody AddressBook addressBook,HttpSession session){
        //通过线程来获取项目的id
        //通过LambdaUpdateWrapper的set方法进行修改
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, session.getAttribute("user") );
        //将该用户中所有的地址修改不是默认地址
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        //将需要设置默认地址进行修改
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /**
     * 编辑收获地址的回显数据
     * */
    @GetMapping("/{id}")
    public R<AddressBook> showAddress(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改收获地址的信息
     * */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /**
     * 删除地址的信息
     * */
    @DeleteMapping
    public R deleteAddress(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }
}

